package com.atguigu.flink.chapter07_state;

import com.atguigu.flink.pojo.MyUtil;
import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 * Created by Smexy on 2022/10/28
 *
 * 针对每种传感器输出最高的3个水位值
 *
 *
 */
public class Demo6_ListState
{
    public static void main(String[] args) {


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env
           .socketTextStream("hadoop103", 8888)
           .map(new MapFunction<String, WaterSensor>()
           {
               @Override
               public WaterSensor map(String value) throws Exception {
                   String[] data = value.split(",");
                   return new WaterSensor(
                       data[0],
                       Long.valueOf(data[1]),
                       Integer.valueOf(data[2])
                   );
               }
           })
           .keyBy(WaterSensor::getId)
           .process(new KeyedProcessFunction<String, WaterSensor, String>()
           {


               private ListState<Integer> top3Vc;

               //task启动时，只执行一次。 获取状态
               @Override
               public void open(Configuration parameters) throws Exception {
                   top3Vc = getRuntimeContext().getListState(new ListStateDescriptor<Integer>("top3Vc", Integer.class));
               }

               /*
                                   WaterSensor value: 当前数据
                               */
               @Override
               public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {

                   //来一条就加入集合
                  top3Vc.add(value.getVc());

                  //取前三   ListState 转为 List才能排序
                   List<Integer> top3VcList = StreamSupport.stream(this.top3Vc.get().spliterator(), true)
                                                        .sorted((vc1, vc2) -> -vc1.compareTo(vc2))
                                                        .limit(3)
                                                        .collect(Collectors.toList());

                   //只把前三覆盖写进去
                  top3Vc.update(top3VcList);

                   out.collect(ctx.getCurrentKey() + "前三水位:" +top3VcList.toString());
                   out.collect(value.toString());

               }
           }).print();

        try {
                    env.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }

    }
}
